<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use App\Models\WxUser as User;
class PhoneController extends Controller
{
    /*
     * 手机号登录
     */
    public function phone(Request  $request)
    {
        $data=$request->get('phone');
        $statusStr = array(
            "0" => "短信发送成功",
            "-1" => "参数不全",
            "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
            "30" => "密码错误",
            "40" => "账号不存在",
            "41" => "余额不足",
            "42" => "帐户已过期",
            "43" => "IP地址限制",
            "50" => "内容含有敏感词"
        );
        $yzm=rand(1111,9999);
        $smsapi = "http://api.smsbao.com/";
        $user = "lh259486"; //短信平台帐号
        $pass = md5("lh259486"); //短信平台密码
        $content="您的验证码是$yzm,请不要告诉任何人";//要发送的短信内容
//        $phone = $data;//要发送短信的手机号码
//        $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
//        $result =file_get_contents($sendurl) ;
//        echo $statusStr[$result];
        $code=Cache::set($data,$yzm);
        try {
            return ['code'=>'200','data'=>$yzm,'msg'=>'短信发送成功'];
        }catch (\Exception $e){
            return ['code'=>'400','data'=>'','msg'=>'短信发送失败'];
        }
    }

    /**
     * @return array|string[]
     * 验证手机号登录
     */
    public function loginDo()
    {
        $data=\request()->post();
        $code=Cache::get($data['phone']);
        if($code!==$data['code']){
            return ['code'=>'400','data'=>'','msg'=>'短信验证码错误'];
        }
        $res=User::where('phone',$data['phone'])->get()->toArray();
        if (empty($res)){
            $user = User::where('id',1)->update([
                'phone'=>$data['phone']
            ]);
            return ['code'=>'200','msg'=>'注册成功','data'=>$user];
        }else{
            $user=User::where('phone',$data['phone'])->get();
            return ['code'=>'300','msg'=>'登录成功','data'=>$user];
        }
    }
}
